package pe.com.drokasa.android.client.service.helper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Clase utilitaria para acceder a la base de datos de productos y 
 * realizar operacioes de consulta.
 * 
 * @author Synopsis S.A
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper 
{
	private static final String DATABASE_NAME = "drokasaDB";
	protected static final String TABLA_PRODUCTO = "Producto";
	protected static final String COLUMNA_CODIGO = "codigo";
	protected static final String COLUMNA_LINEA = "linea";
	protected static final String COLUMNA_NOMBRE = "nombre";
	protected static final String COLUMNA_PRECIO = "precio";
	private String DATABASE_PATH;
	
	protected SQLiteDatabase database;
	private Context context;
	
	/**
	 * @param context
	 */
	public DatabaseHelper(Context context)
	{
		super(context, DATABASE_NAME, null, 33);
		this.context = context;
		this.DATABASE_PATH = context.getFilesDir().getPath().replace("files", "databases/");
		
		createDatabase();
	}
	
	/**
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase database) 
	{
		
	}

	/**
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) 
	{
		createDatabase();
	}
	
	/**
	 * @throws IOException
	 */
	private void createDatabase()
	{
		boolean dbExist = checkDatabase();
		if(!dbExist)
		{
			this.getReadableDatabase();
			try 
			{
				copyDataBase();
			} 
			catch (IOException e) 
			{
				throw new Error("Ocurrio un error al copiar la base de datos.");
			}
		}
	}
	
	/**
	 * 
	 */
	protected void openDatabase()
	{
		 String path = DATABASE_PATH + DATABASE_NAME;
		 database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
	}
	
	/**
	 * 
	 */
	protected synchronized void closeDatabase()
	{
		if(database != null)
		{
			database.close();
		}
		super.close();
	}
	
	/**
	 * @return
	 */
	private boolean checkDatabase()
	{
		SQLiteDatabase checkDB = null;
		try 
		{
			String path = DATABASE_PATH + DATABASE_NAME;
			checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		} 
		catch (SQLiteException e) {}
		
		if(checkDB != null) { checkDB.close(); }
		 
		return checkDB != null ? true : false;
	}
	
	/**
	 * @throws IOException
	 */
	private void copyDataBase() throws IOException
	{
		InputStream input = context.getAssets().open(DATABASE_NAME);
		 
		String filename = DATABASE_PATH + DATABASE_NAME;
		 
		OutputStream output = new FileOutputStream(filename);
		 
		byte[] buffer = new byte[1024];
		int length;
		while ((length = input.read(buffer))>0)
		{
			output.write(buffer, 0, length);
		}
		 
		output.flush();
		output.close();
		input.close();
	}
}